package com.myproject.http;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description: //TODO  简单的HTTP服务器， 其实就是接受HTTP请求，并解析请求，
 * @Author: sunfch
 * @Date: 2019/1/3 16:35
 * @Param:
 * @Return:
 */
public class HttpServer1 {

    public static void main(String[] args) {


        try {

            /*监听端口号，只要是8888就能接收到*/
            ServerSocket ss = new ServerSocket(9960);

            while (true){
                Socket socket = ss.accept();
                ClientHandler clientHandler = new ClientHandler(socket);
                clientHandler.start();
                System.out.println(socket.toString()+": accept");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class ClientHandler extends Thread{

        private Socket socket;

        public ClientHandler(Socket socket){
            this.socket = socket;
        }

        @Override
        public void run() {

            try {
                /*获取客户端输入流，就是请求过来的基本信息：请求头，换行符，请求体*/
                InputStream inputStream = socket.getInputStream();
                BufferedReader bd = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));

                /**
                 * 接受HTTP请求，并解析数据
                 */
                String requestHeader;
                int contentLength = 0;
                while ((requestHeader = bd.readLine()) != null && !requestHeader.isEmpty()) {
                    System.out.println(requestHeader);
                    /**
                     * 获得GET参数
                     */
                    if (requestHeader.startsWith("GET")) {
                        int begin = requestHeader.indexOf("/?") + 2;
                        int end = requestHeader.indexOf("HTTP/");
                        String condition = requestHeader.substring(begin, end);
                        System.out.println("GET参数是：" + condition);
                    }
                    /**
                     * 获得POST参数
                     * 1.获取请求内容长度
                     */
                    if (requestHeader.startsWith("Content-Length")) {
                        int begin = requestHeader.indexOf("Content-Lengh:") + "Content-Length: ".length();
                        String postParamterLength = requestHeader.substring(begin).trim();
                        contentLength = Integer.parseInt(postParamterLength);
                        System.out.println("POST参数长度是：" + Integer.parseInt(postParamterLength));
                    }
                }
                StringBuffer sb = new StringBuffer();
                if (contentLength > 0) {
                    for (int i = 0; i < contentLength; i++) {
                        System.err.println(i);
                        sb.append((char) bd.read());
                        if (sb.toString().getBytes("UTF-8").length==contentLength){
                            break;
                        }
                    }
                    System.out.println("POST参数是：" + sb.toString());
                }
                /*发送回执*/
                PrintWriter pw = new PrintWriter(socket.getOutputStream());

                pw.println("HTTP/1.1 200 OK");
                pw.println("Content-type:text/html");
                pw.println();
                pw.println("<h1>successful</h1>");

                pw.flush();
                socket.close();
            } catch (Exception e){
                System.err.println(Thread.currentThread().getName()+"连接异常断开"+e.getMessage());
                e.printStackTrace();
            }


        }
    }
}